const merge = require('webpack-merge');
const common = require('./webpack.common.js');
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

// 清除目录
const CleanWebpackPlugin = require('clean-webpack-plugin');
// css 压缩
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
// 代码压缩
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');

module.exports = merge(common, {
  mode: 'production',
  output: {
    path: path.resolve(__dirname, './dist'),
    // 打包多出口文件
    filename: './js/[name].[hash].js',
    publicPath: './'
  },
  // devtool: 'cheap-module-eval-source-map', // 开发模式调试定位错误文件位置
  plugins: [
    //删除dist目录
    new CleanWebpackPlugin({
      // root: path.resolve(__dirname, './'), //根目录
      // verbose Write logs to console.
      verbose: true, //开启在控制台输出信息
      // dry Use boolean "true" to test/emulate delete. (will not remove files).
      // Default: false - remove files
      dry: false,
    }),
    new webpack.DefinePlugin({
      // DefinePlugin 允许创建一个在编译时可以配置的全局常量
      'process.env.BASE_URL': JSON.stringify('production')
    }),
    // 分离css插件参数为提取出去的路径
    new ExtractTextPlugin({
      filename: 'css/[name].[hash:8].min.css',
    }),
    //压缩css
    new OptimizeCSSPlugin({
      cssProcessorOptions: {
        safe: true
      }
    }),
    //上线压缩 去除console等信息webpack4.x之后去除了webpack.optimize.UglifyJsPlugin
    new UglifyJSPlugin({
      uglifyOptions: {
        exclude:['/node_modules/'],
        compress:{
          warnings: true, // 是否显示警告信息
          drop_console: true, // 是否显示console
          drop_debugger: true, // debugger
        },
        output:{ // 压缩输出配置
          comments: false
        }
      }
    })
  ]
});